package com.sx.zongzhi.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.PictureAdapter;
import com.sx.zongzhi.bean.KeyPunishBean;
import com.sx.zongzhi.bean.KeyPunishDetailBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.presenter.KeyPunishDetailPest;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IKeyPunishDetailView;
import com.sx.zongzhi.widget.CustomDialog;
import com.sx.zongzhi.widget.LoadDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 重点地区详情
 */
public class KeyPunishDetailActivity extends BaseActivity implements IKeyPunishDetailView {

    @BindView(R.id.tv_PunishOrder) TextView tv_PunishOrder;
    @BindView(R.id.tv_OrderProblem) TextView tv_OrderProblem;
    @BindView(R.id.tv_InvolveType) TextView tv_InvolveType;
    @BindView(R.id.tv_InvolveRange) TextView tv_InvolveRange;
    @BindView(R.id.tv_LeaderComp) TextView tv_LeaderComp;
    @BindView(R.id.tv_JoinComp) TextView tv_JoinComp;
    @BindView(R.id.tv_PunishLeader) TextView tv_PunishLeader;
    @BindView(R.id.tv_leaderTel) TextView tv_leaderTel;
    @BindView(R.id.tv_LeaderPhone) TextView tv_LeaderPhone;
    @BindView(R.id.tv_PunishTime) TextView tv_PunishTime;
    @BindView(R.id.tv_UncoverNum) TextView tv_UncoverNum;
    @BindView(R.id.tv_FindNum) TextView tv_FindNum;
    @BindView(R.id.tv_PunishState) TextView tv_PunishState;
    @BindView(R.id.tv_Evaluate) TextView tv_Evaluate;
    @BindView(R.id.recyclerView) RecyclerView recyclerView;

    /**重点地区id*/
    private String keyPunishId="";
    private String orgId="";
    private KeyPunishDetailPest detailPest;
    private KeyPunishDetailBean detailBean;
    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_key_punish_detail);
        ButterKnife.bind(this);
        setTitleName(R.string.punish_detail);
        keyPunishId=getIntent().getStringExtra("keyPunishId");
        orgId = getIntent().getStringExtra("orgId");
        initViews();
        initData();
    }

    private void initViews() {
        KeyPunishBean.PermissionBean userPermission = (KeyPunishBean.PermissionBean)
                getIntent().getSerializableExtra("userPermission");
        if (userPermission != null && userPermission.isDeleteSocialSecurity()){
            setRightImage(R.mipmap.icon_nav_delete);
        }
        if (userPermission != null && userPermission.isUpdateSocialSecurity()){
            setRightImage2(R.mipmap.icon_nav_edit);
        }
    }

    private void initData() {
        detailPest=new KeyPunishDetailPest(this);
        LoadDialog.show(this);
        detailPest.getKeyPunishDetail(keyPunishId);
    }



    @OnClick({R.id.imv_Right,R.id.imv_Right2})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.imv_Right://删除
                createDelete();
                break;
            case R.id.imv_Right2://编辑
                if (detailBean!=null){
                    startActivity(new Intent(this,KeyPunishEditActivity.class)
                            .putExtra("keyPunishId",keyPunishId)
                            .putExtra("KeyPunishDetailBean",detailBean)
                            .putExtra("orgId",orgId));
                    finish();
                }else {
                    ToastUtils.showToast(this,"未获取到详情 编辑失败");
                }
                break;
        }
    }

    private void createDelete() {
        CustomDialog.Builder builder=new CustomDialog.Builder(this);
        CustomDialog dialog=builder.setMessage("确定删除该行业信息?").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                LoadDialog.show(KeyPunishDetailActivity.this,"删除中");
                detailPest.KeyPunishDelete(keyPunishId);
                dialog.dismiss();
            }
        }).create();
        dialog.show();
    }


    @Override
    public void showError(String message) {
        LoadDialog.dismiss(this);
        ToastUtils.showToast(this,message);
    }

    @Override
    public void getKeyPunishDetail(KeyPunishDetailBean detailBean) {
        LoadDialog.dismiss(this);
        if (detailBean!=null){
            this.detailBean=detailBean;
            tv_PunishOrder.setText(detailBean.getSecurityKeyAreas());
            tv_OrderProblem.setText(detailBean.getSecurityProblems()!=null?
                        detailBean.getSecurityProblems().getDisplayName():"未知");
            tv_InvolveType.setText(detailBean.getInvolvedType()!=null ?
                    detailBean.getInvolvedType().getDisplayName():"未知");
            tv_InvolveRange.setText(detailBean.getRelateArea());
            tv_LeaderComp.setText(detailBean.getRepairLeaderUnit());
            tv_JoinComp.setText(detailBean.getRepairPartakeUnit());
            tv_PunishLeader.setText(detailBean.getLeaderUnitDirector());
            tv_leaderTel.setText(detailBean.getLeaderUnitDirectorTelephone());
            tv_LeaderPhone.setText(detailBean.getLeaderUnitDirectorMobileNumber());
            tv_PunishTime.setText(detailBean.getRepairTimelimit());
            tv_UncoverNum.setText(detailBean.getInvestigatePunishNumber()+"");
            tv_FindNum.setText(detailBean.getSolveNumber()+"");
            tv_PunishState.setText(detailBean.getRepairSituation());
            tv_Evaluate.setText(detailBean.getEffectEvaluate()!=null ?
                    detailBean.getEffectEvaluate().getDisplayName():"未知");
            if (detailBean.getFiles()!=null&&detailBean.getFiles().size()>0){
                recyclerView.setVisibility(View.VISIBLE);
                recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayout.HORIZONTAL, false));
                PictureAdapter pictureAdapter = new PictureAdapter(this, detailBean.getFiles());
                recyclerView.setAdapter(pictureAdapter);
            }
        }
    }

    @Override
    public void KeyPunishDelete(String str) {
        LoadDialog.dismiss(this);
        ToastUtils.showToast(this,str);
        MyRxBus.get().post(new MsgEvent("KeyPunishActivity",1,str));
        finish();
    }
}
